package com.taoyi.mhouse.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.taoyi.mhouse.R;
import com.taoyi.mhouse.adapter.AnimeHomeAdapter;
import com.taoyi.mhouse.binder.BannerBinder;
import com.taoyi.mhouse.binder.SixAnimeBinder;
import com.taoyi.mhouse.binder.SearchBinder;
import com.taoyi.mhouse.binder.ShoopingEntryBinder;
import com.taoyi.mhouse.binder.TwoAnimeBinder;
import com.taoyi.mhouse.util.UIUtils;

import java.util.Random;

/**
 * Created by tempo on 2015/11/11.
 */
public class AnimeFragment extends LoadingFreshingFragment {

    private View main;
    private RecyclerView rvHome;
    private RecyclerView.LayoutManager layoutManager;
    private AnimeHomeAdapter mAdapter;
    Handler handler=new Handler();

    private String data;
    @Override
    public void loadData(final LoadingCallback callback) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Random rand = new Random();
                int i= rand.nextInt(5);
                if(i==0) {
                    callback.setResult(ResultState.Empty,null);
                }else if(i==1){
                    callback.setResult(ResultState.Error,null);
                }else{
                    data = "ok success";
                    callback.setResult(ResultState.Success, data);
                }

            }
        },800);
    }

    @Override
    public View getSuccessView(View parent, Object data) {
        main = LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.fragment_anime, null);
        rvHome = (RecyclerView) main.findViewById(R.id.rv_home);

        rvHome.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(UIUtils.getContext());
        rvHome.setLayoutManager(layoutManager);
        init();
        return main;
    }



    @Override
    protected void clearData() {
        data=null;
    }

    public void init() {
        mAdapter = new AnimeHomeAdapter();
        mAdapter.putBinder(AnimeHomeAdapter.ViewType.SEARCH,new SearchBinder(mAdapter));
        mAdapter.putBinder(AnimeHomeAdapter.ViewType.BANNER,new BannerBinder(mAdapter));
        mAdapter.putBinder(AnimeHomeAdapter.ViewType.ENTRY,new ShoopingEntryBinder(mAdapter));
        mAdapter.putBinder(AnimeHomeAdapter.ViewType.SIX_ANIME, new SixAnimeBinder(mAdapter));
        mAdapter.putBinder(AnimeHomeAdapter.ViewType.TWO_ANIME,new TwoAnimeBinder(mAdapter));
        rvHome.setAdapter(mAdapter);
    }


}
